public class SolutionLCP62 {
    public int transportationHub(int[][] path) {
        int max=0;
        for (int[] ints : path) {
            max=Math.max(ints[0],max);
            max=Math.max(ints[1],max);
        }
        int[] ru=new int[max+1];
        int[] chu=new int[max+1];
        for (int[] ints : path) {
            ru[ints[1]]++;
            chu[ints[0]]++;
        }
        for (int i=0;i<=max;i++){
            if (ru[i]==max && chu[i]==0){
                return i;
            }
        }
        return -1;
    }

    public static void main(String[] args) {
        System.out.println(new SolutionLCP62().transportationHub(new int[][]{{0,1},{0,3},{1,3},{2,0},{2,3}}));
    }
}
